﻿using Core.DTO.System.SysModule;
using Core.IService;
using Core.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.WebApi.Controllers.SystemControllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ModuleController : ControllerBase
    {

        public ModuleController(ISysModuleService service)
        {
            Service = service;
        }

        public ISysModuleService Service { get; }

        [HttpPost]
        public async Task<ActionResult<SysModule>> Post(SysModulePostDTO dto)
        {
            await Service.AddAsync(dto);
            return CreatedAtAction("", new { Id = dto.Id }, dto);
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<SysModule>> Get(int id)
        {
            return await Service.GetEntityAsync(id);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> Put(int id, SysModulePostDTO dto)
        {
            if (id != dto.Id)
                return BadRequest();
            await Service.EditAsync(dto);
            return NoContent();

        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(int id)
        {
            await Service.MarkDeleteAsync(id);
            return NoContent();
        }

    }
}
